package attack.arfok;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.C;
import framework.Util;
import framework.movements.LittleBackwardMovement;
import attack.nartan.FireBall;

public class PoisonBall extends FireBall {

	/** The time in ms when it last hits */
	protected int lastHit;
	
	protected final static int WIND_UP = 400;
	
	private final static int SPEED = 300;
	
	public PoisonBall() {
		this(null);
	}
	
	public PoisonBall(BasicFighter fighter) {
		super(fighter);
		AnimationManager n = fighter.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[1];
		frames[0] = this.spriteSheet.getSprite(0, 3);
		this.ball = new Animation(frames, 50);
		this.ballBackward = Util.getFlippedAnimation(this.ball, true, false);
		this.impact = new Animation();
		this.impact.addFrame(spriteSheet.getSprite(1, 4), 300);
		
		frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		frames[1] = this.spriteSheet.getSprite(2, 2);
		frames[2] = this.spriteSheet.getSprite(2, 2);
		this.attackAnimation = new Animation(frames, 120);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward.setLooping(false);
		frames[0] = this.spriteSheet.getSprite(3, 2);
		frames[1] = this.spriteSheet.getSprite(4, 2);
		frames[2] = this.spriteSheet.getSprite(4, 2);
		this.attackJumpAnimation = new Animation(frames, 120);
		this.attackJumpAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackJumpAnimation.setLooping(false);
		this.attackJumpAnimationBackward.setLooping(false);
		
		this.attacker = fighter;
		this.setWidth((int)fighter.getWidth());
		this.setHeight((int)fighter.getHeight());
		
		try {
			this.hitSound = new Sound(C.ASSETS_FOLDER + "hit_poison.ogg");
			this.parrySound = new Sound(C.ASSETS_FOLDER + "parry.ogg");
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void trigger() {
		//if (!getAttacker().isJumping()) {
			super.trigger();
			this.completed = false;
			this.hit = false;
			this.interruptOnBoundHit = false;
			Rectangle hitbox = getAttacker().getHitBox();
			setXspeed(getAttacker().isForward() ? SPEED : -SPEED);
			setY((int)(getAttacker().getY()));
			if (getAttacker().isForward()) {
				this.currentAttackAnimation = getAttacker().isJumping() ? this.attackJumpAnimation : this.attackAnimation;
				this.currentBall = this.ball;
				setX((int)(hitbox.getX() - hitbox.getWidth() * 6));
			} else {
				this.currentAttackAnimation = getAttacker().isJumping() ? this.attackJumpAnimationBackward : this.attackAnimationBackward;
				this.currentBall = this.ballBackward;
				setX((int)(hitbox.getX() - hitbox.getWidth() * 2));
			}
			this.currentAttackAnimation.restart();
			getAttacker().setYspeed(Math.abs(getAttacker().getYspeed()));
			lastHit = -WIND_UP;
		//}
	}
	
	@Override
	public void draw(Graphics g) {
		// notification that the preparing animation has been completed
		completed = this.currentAttackAnimation.getFrame() == this.currentAttackAnimation.getFrameCount() - 1;
		if(isDrawingAttacker()) {
			this.currentAttackAnimation.draw(getAttacker().getX(), getAttacker().getY(), getWidth(), getHeight());
		}
		if (this.hasHit()) {
			//this.impact.draw((float)collision.getX()-100, (float)collision.getY()-10);
		}
		if (this.currentAttackAnimation.getFrame() == this.currentAttackAnimation.getFrameCount() - 1) {
			Image img = this.currentBall.getCurrentFrame();
			img.setAlpha(1.3f - (float)currentTime / (float)getDuration());
			img.draw(getX(), getY(), getWidth(), getHeight());
		}
	}
	
	
	
	@Override
	public int getPower() {
		return 3;
	}	

	@Override
	public int getDuration() {
		return 1500;
	}
	
	@Override
	public Rectangle getHitBox() {
		int offset = getWidth() / 6;
		Rectangle hitBox;
		hitBox = new Rectangle(-1000, -1000, 0, 0);
		if (this.currentAttackAnimation.getFrame() == this.currentAttackAnimation.getFrameCount() - 2 || !isBlocking()) {
			if (this.currentBall == this.ball) {
				hitBox = new Rectangle((int)(getX() + 3 * offset), getY() + offset, 3 * offset, offset);	
			} else {
				hitBox = new Rectangle((int)(getX() ), getY() + offset, 3 * offset, offset);	
			}
		} 
		return hitBox;
	}
	
	@Override
	public void onHit(BasicFighter f, Rectangle2D collision) {
		int forward = f.isForward() ? -1 : 1;
		if (!super.hasHit()) {
			
		}
		if (interruptOnHit &&  f.getCurrentAttack().isInterruptable()) {
		    f.getCurrentAttack().interrupt();
		}
		if (lastHit + WIND_UP < currentTime) {
			if (f.isParrying()) {
				f.parry(getPower());
			} else {
				f.hit(getPower());
				this.hitSound.stop();	
				this.hitSound.play();
			}
			lastHit = currentTime;
		}
		LittleBackwardMovement move = new LittleBackwardMovement(f);
		f.move(move);
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
	}

	@Override
	public boolean isBlocking() {
		return super.isBlocking();
	}

	@Override
	public boolean isDrawingAttacker() {
		return isActive() && this.currentAttackAnimation.getFrame() <= this.currentAttackAnimation.getFrameCount() - 2;
	}
}
